package com.sinven.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
public class PageAction extends ActionSupport{

    private static final String url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=image";
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    @Action(value = "upload",results = {@Result(location = "/upload.jsp")})
    public String showUpload() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String method = request.getMethod();
        if("get".equalsIgnoreCase(method)){
            return SUCCESS;
        }else{
           
            String posturl = url.replace("ACCESS_TOKEN","24_s5djVyNl2IPselQej9sXNJbkP1Gnn8GOZzgAlnOr9w_Vt1teclKNDHEpF98YH7B2URgkEmaHy87Kf3KtKOZ64cyzfXl5E0tcIRZH_uordaZ6MY4u6-yc85mezCDcBjnXJf5mGAbsEAGsNMYzLTOaAIASAV");

            HttpPost post = new HttpPost(posturl);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            FileBody bin = new FileBody(new File("D://无标题.png"));

            builder.addPart("file",bin);

            HttpEntity entity = builder.build();

            post.setEntity(entity);

            CloseableHttpClient client = HttpClients.createDefault();

            CloseableHttpResponse response = client.execute(post);

            HttpEntity responseEntity = response.getEntity();

            System.out.println("返回值："+EntityUtils.toString(responseEntity));

            return  NONE;
        }

    }
}
